<template>
  <div class="body">
    <el-table :data="baseList" style="width: 100%">
      <el-table-column prop="number" label="试题编号"> </el-table-column>
      <el-table-column prop="subject" label="学科"> </el-table-column>
      <el-table-column prop="catalog" label="目录"> </el-table-column>
      <el-table-column label="题型">
        <template slot-scope="{ row }">
          <span v-if="row.questionType === '1'">单选</span>
          <span v-if="row.questionType === '2'">多选</span>
          <span v-if="row.questionType === '3'">简答</span>
        </template>
      </el-table-column>
      <el-table-column prop="question" label="题干">
        <template slot-scope="{ row }">
          <span v-html="row.question"></span>
        </template>
      </el-table-column>
      <el-table-column label="录入时间" width="180">
        <template slot-scope="{ row }">
          {{ row.addDate | formatTimeXxh }}
        </template>
      </el-table-column>
      <el-table-column prop="difficulty" label="难度">
        <template slot-scope="{ row }">
          <span v-if="row.difficulty === '1'">简单</span>
          <span v-if="row.difficulty === '2'">一般</span>
          <span v-if="row.difficulty === '3'">困难</span>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="{ row }">
          <el-tooltip
            class="item-pre"
            effect="dark"
            content="预览"
            placement="bottom-start"
          >
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-view"
              circle
              plain
              @click="preFn(row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item-edit"
            effect="dark"
            content="修改"
            placement="bottom-start"
          >
            <el-button
              type="success"
              size="mini"
              icon="el-icon-edit"
              circle
              @click="jump(row.id)"
              plain
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item-del"
            effect="dark"
            content="删除"
            placement="bottom-start"
          >
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              circle
              @click="delFn(row)"
              plain
            ></el-button>
          </el-tooltip>
          <el-tooltip
            class="item-addF"
            effect="dark"
            content="加入精选"
            placement="bottom-start"
          >
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-check"
              circle
              @click="addFav(row)"
              plain
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 预览页面弹窗 -->
    <el-dialog
      title="题目预览"
      :visible.sync="preDialogVisible"
      width="60%"
      @close="onClose"
      class="questions-preview"
    >
      <!-- 预览第一行 -->
      <el-row class="first">
        <el-col :span="6"
          >【题型】:
          <span v-if="preForm.questionType === '1'">单选题</span>
          <span v-if="preForm.questionType === '2'">多选题</span>
          <span v-if="preForm.questionType === '3'">简答题</span>
        </el-col>
        <el-col :span="6">【编号】: {{ preForm.id }}</el-col>
        <el-col :span="6"
          >【难度】:
          <span v-if="preForm.difficulty === '1'">简单</span>
          <span v-if="preForm.difficulty === '2'">一般</span>
          <span v-if="preForm.difficulty === '3'">困难</span>
        </el-col>
        <el-col :span="6">【标签】: {{ preForm.tags }}</el-col>
        <el-col :span="6">【学科】: {{ preForm.subjectName }}</el-col>
        <el-col :span="6">【目录】: {{ preForm.directoryName }}</el-col>
        <el-col :span="6">【方向】: {{ preForm.direction }}</el-col>
      </el-row>
      <hr />
      <!-- 预览第二行 -->
      <el-row>
        <div>【题干】:</div>
        <span id="showTxt" ref="showTxt"></span>
        <div>
          <span v-if="preForm.questionType === '1'">单选题</span>
          <span v-if="preForm.questionType === '2'">多选题</span>
          <span
            v-if="preForm.questionType === '2' || preForm.questionType === '1'"
            >选项:（以下选中的选项为正确答案）</span
          >
        </div>
        <!-- 单选显示 -->
        <div v-if="preForm.questionType === '1'">
          <div style="margin: 10px 0" v-for="item in radioList" :key="item.id">
            <el-radio :value="radio" :label="item.isRight">{{
              item.title
            }}</el-radio>
          </div>
        </div>
        <!-- 多选显示 -->
        <div v-if="preForm.questionType === '2'">
          <div v-for="item in radioList" :key="item.id">
            <!-- :checked='item.isRight===1?true:false' -->
            <el-checkbox :value="item.isRight === 1 ? true : false">{{
              item.title
            }}</el-checkbox>
          </div>
        </div>
      </el-row>
      <hr />
      <!-- 预览第三行 -->
      <el-row>
        <div>
          【参考答案】:
          <span
            ><el-button type="danger" size="mini" @click="isShowVideo = true"
              >视频答案预览</el-button
            ></span
          >
        </div>
        <video
          controls="controls"
          :src="preForm.videoURL"
          v-if="isShowVideo"
          autoplay
        ></video>
      </el-row>
      <hr />
      <el-row>
        <div>
          【答案解析】:
          <div style="display: inline-block" v-html="preForm.answer"></div>
        </div>
      </el-row>
      <hr />
      <el-row>
        <div><span>【题目备注】:</span><span ref="tips"></span></div>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onClose">关闭</el-button>
      </span>
    </el-dialog>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="reqForm.page"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="reqForm.pagesize"
      layout="prev, pager, next, sizes, jumper"
      :total="total"
      background
    >
    </el-pagination>
  </div>
</template>

<script>
import { detail, remove, choiceAdd } from '@/api/hmmm/questions'
export default {
  name: 'BaseFormBody',
  props: {
    reqForm: {
      type: Object,
      required: true
    },
    baseList: {
      type: Array,
      required: true
    },
    total: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      preDialogVisible: false,
      preForm: {},
      radioList: [],
      radio: 1,
      checkedBoxList: [],
      isShowVideo: false
    }
  },

  created () {},

  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.reqForm.pagesize = val
      this.$emit('loadList')
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.reqForm.page = val
      this.$emit('loadList')
    },
    // 点击预览触发函数
    async preFn (row) {
      console.log(row)
      const res = await detail(row)
      console.log(2, res)
      this.preForm = res.data
      this.preDialogVisible = true
      this.$nextTick(() => {
        // 等待弹窗里面的span dom 加载完成
        this.$refs.showTxt.innerHTML = res.data.question
        this.radioList = res.data.options
        this.$refs.answer.innerHTML = res.data.answer
        this.$refs.tips.innerHTML = res.data.remarks
      })
      const newArr = []
      res.data.options.forEach((item) => {
        if (item.isRight === 1) {
          newArr.push(item.title)
        }
      })
      console.log(newArr)
    },
    // 点击删除图标触发函数
    delFn (row) {
      this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.$emit('loadList')
        })
        .catch(() => {})
    },
    // 点击勾添加精选
    async addFav (row) {
      console.log(row)
      const newObj = JSON.parse(JSON.stringify(row))
      newObj.choiceState = 1

      this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      })
        .then(async () => {
          await choiceAdd(newObj)
          this.$message({
            type: 'success',
            message: '加入精选成功!'
          })
          this.$emit('loadList')
        })
        .catch(() => {})
    },
    // 点击关闭的时候触发的事件
    onClose () {
      this.preDialogVisible = false
      this.isShowVideo = false
    },
    // edit
    jump (id) {
      this.$router.push({
        path: '/questions/new',
        query: {
          id: id
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
#showTxt {
  color: blue;
}
.dialog-footer {
  display: flex;
  justify-content: end;
}
.questions-preview .el-col {
  padding: 10px 0;
}
</style>
